<template>
  <div class="container">
    <div class="apply-container">
      <div class="apply-container-head"></div>
      <div class="apply-container-body">
        <div class="apply-container-body-title">
          <div class="apply-container-body-title-font">添加驾驶员</div>
          <div class="apply-container-body-title-return">
            <el-button size="small" @click="goBackPage">返回</el-button>
          </div>
        </div>
        <el-divider class="apply-container-body-title-line"></el-divider>
        <div class="apply-container-body-content">
          <el-form :model="DriverArchives" label-width="110px" label-position="LabelPosition" ref="DriverArchives"
            :rules="DriverArchivesRules" style="width: 100%;">
            <el-form-item label="可用工号:" prop="SelectUserId" style="width: 40%;">
              <el-select v-model="DriverArchives.SelectUserId" placeholder="请选择工号类型" @change="getUserNameAndPhone"
                clearable>
                <el-option v-for="(item, $index) in this.UserNameRole" :key="$index" :label="item" :value="item">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="姓名:" prop="InputDriverName" style="width: 40%;">
              <el-input v-model="DriverArchives.InputDriverName" placeholder="请输入姓名" :disabled="true"
                clearable></el-input>
            </el-form-item>

            <el-form-item label="驾照类型:" prop="SelectLicenseType" style="width: 40%;">
              <el-select v-model="DriverArchives.SelectLicenseType" placeholder="请输入驾照类型" clearable>
                <el-option label="A1" value="A1"></el-option>
                <el-option label="A2" value="A2"></el-option>
                <el-option label="A3" value="A3"></el-option>
                <el-option label="B1" value="B1"></el-option>
                <el-option label="B2" value="B2"></el-option>
                <el-option label="C1" value="C1"></el-option>
                <el-option label="C2" value="C2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="驾龄:" prop="InputDrivingAge" style="width: 40%;">
              <el-input v-model="DriverArchives.InputDrivingAge" placeholder="请输入驾龄" clearable></el-input>
            </el-form-item>
            <el-form-item label="电话:" prop="InputDriverPhone" style="width: 40%;">
              <el-input v-model="DriverArchives.InputDriverPhone" placeholder="请输入电话" :disabled="true"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="身份证号:" prop="InputDriverIdNumber" style="width: 40%;">
              <el-input v-model="DriverArchives.InputDriverIdNumber" placeholder="请输入身份证号" :disabled="true"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="驾驶证有效期:" prop="DriverLicenseValidity" style="width: 40%;">
              <el-date-picker v-model="DriverArchives.DriverLicenseValidity" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择驾驶证截止日期" clearable></el-date-picker>
            </el-form-item>
            <div class="apply-container-body-content-submit">
              <el-button type="primary" @click="DriverArchiveInsert('DriverArchives')">提交</el-button>
            </div>
          </el-form>
        </div>
      </div>
      <div class="apply-container-footer"></div>
    </div>
  </div>
</template>

<script>
import { driverArchiveInsert } from "@/api/archives";
import { getAvailableDriverRole } from "@/api/archives"
import { getUserInfoById } from "@/api/user"

export default {
  name: 'AddDriverArchivesView',
  data() {
    return {
      LabelPosition: 'right',
      DriverArchives: {
        InputDriverName: null,
        SelectLicenseType: null,
        InputDrivingAge: null,
        InputDriverPhone: null,
        InputDriverIdNumber: null,
        DriverLicenseValidity: null,
        SelectUserId: null
      },
      DriverArchivesRules: {
        InputDriverName: [{ required: true, message: '请输入驾驶员姓名', trigger: 'blur' },],
        SelectLicenseType: [{ required: true, message: '请选择驾驶证类型', trigger: 'blur' },],
        SelectUserId: [{ required: true, message: '请选择驾驶员工号', trigger: 'blur' },],
        InputDrivingAge: [{ required: true, message: '请输入驾龄', trigger: 'blur' },],
        InputDriverPhone: [{ required: true, message: '请输入驾驶员联系方式', trigger: 'blur' },],
        InputDriverIdNumber: [{ required: true, message: '请输入驾驶员身份证号', trigger: 'blur' },],
        DriverLicenseValidity: [{ required: true, message: '请输入驾驶证有效期', trigger: 'blur' },],
      },
      SubmitDriverArchive: {
        userId: null,
        driverName: null,
        licenseType: null,
        drivingAge: null,
        driverPhone: null,
        driverIdNumber: null,
        driverLicenseValidity: null
      },
      UserNameRole: []
    }
  },

  methods: {
    getUserNameRole() {
      getAvailableDriverRole().then(res => {
        console.log(res);
        const temp = res.data.data

        const result = [];
        temp.forEach(element => { result.push(Object.values(element)) });
        result.forEach(element => {
          //console.log(element);
          const driverId = element[0];
          //console.log(driverId);
          this.UserNameRole.push(driverId)

        });
        //console.log(this.UserNameRole);

      }).catch((err) => {
        console.log(err);
      })
    },

    getUserNameAndPhone(newValue) {
      if (newValue == '' || newValue == null) {
        this.DriverArchives.InputDriverName = null
        this.DriverArchives.InputDriverIdNumber = null
        this.DriverArchives.InputDriverPhone = null
      }
      else {
        getUserInfoById(newValue).then(res => {
          let temp = res.data.data[0]
          this.DriverArchives.InputDriverName = temp.userName
          this.DriverArchives.InputDriverIdNumber = temp.userIdNumber
          this.DriverArchives.InputDriverPhone = temp.userPhone
        })
      }
    },

    DriverArchiveInsert(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('此操作将新增此驾驶员档案, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {

            this.SubmitDriverArchive.driverIdNumber = this.DriverArchives.InputDriverIdNumber;
            this.SubmitDriverArchive.userId = this.DriverArchives.SelectUserId;
            this.SubmitDriverArchive.driverName = this.DriverArchives.InputDriverName;
            this.SubmitDriverArchive.driverPhone = this.DriverArchives.InputDriverPhone;
            this.SubmitDriverArchive.driverLicenseValidity = this.DriverArchives.DriverLicenseValidity;
            this.SubmitDriverArchive.drivingAge = this.DriverArchives.InputDrivingAge;
            this.SubmitDriverArchive.licenseType = this.DriverArchives.SelectLicenseType;

            driverArchiveInsert(this.SubmitDriverArchive).then(res => {
              if (res.data.code === '0') {
                this.$message({
                  message: "添加成功",
                  duration: 2000,
                  showClose: true,
                  type: 'success'
                });

                this.goBackPage()

              }
              else {
                this.$message.error(res.data.msg);
              }
            }).catch((err) => {
              console.log(err)
            })

          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            });
          });
        }
        else {
          return false;
        }
      })
    },
    goBackPage() {
      this.$router.go(-1)
    },
  },

  mounted() {
    this.getUserNameRole();
  },
}
</script>

<style scoped>
.container {
  height: 100%;
  width: 95%;
  margin: 0 auto;
  margin-top: 1%;
  background-color: #ffffff;
}

.apply-container {
  width: 90%;
  height: 100%;
  margin: 0 auto;
  /* background-color: #f2f2f2; */

}

.apply-container-head {
  width: 95%;
  height: 30px;
  margin: 0 auto;
}

.apply-container-body {
  width: 95%;
  height: 92%;
  margin: 0 auto;
  background-color: #ffffff;

}

.apply-container-body-title {
  width: 98%;
  height: 5%;
  margin: 0 auto;
  display: flex;
}

.apply-container-body-title-return {
  width: 100%;
  padding-top: 5px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.apply-container-body-title-line {
  height: 1px;
  width: 98%;
  margin: 5px auto;
}

.apply-container-body-title-font {
  width: 100%;
  height: 99%;
  font-size: 18px;
  font-weight: bold;
  text-align: left;
  padding-top: 10px;
}

.apply-container-body-content {
  width: 85%;
  height: 100%;
  margin: 20px auto;
  display: flex;

}

.apply-container-body-content-submit {
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}

.apply-container-footer {
  width: 95%;
  height: 10px;
  margin: 0 auto;
}
</style>